var baseUrl = "http://fgpd.app.jggroup.cn";
let websocketUrl = "ws:fgpd.app.jggroup.cn/ws/swipeCard";
function getToken(){
	let token = uni.getStorageSync("token");
	return token;
}

function setupIp(ip){
	uni.setStorageSync("ip", ip);
	composeUrl();
}

function getIp(){
	return uni.getStorageSync("ip");
}
function setToken(token){
	uni.setStorageSync("token", token);
}

function getHeaders() {
	let header = {
		'token': getToken(),
		'Content-Type': 'application/json'
	}
	return header;
}
function showTip(title,time=500,icon='none'){
	uni.showToast({
		icon:icon,
		title:title,
		duration:time
	})
}

function composeUrl(){
	let ip = getIp();
	if(ip){
		baseUrl = "http://"+ip+":8101";
		websocketUrl = "ws:"+ip+":8101/ws/swipeCard";
	}
	
}

function getWebSocketUrl(){
	return websocketUrl;
}
function request(url, method, param){
	return new Promise((resolve, reject) => {
		uni.request({
			method: method,
			url: baseUrl + url,
			data: param,
			sslVerify: false,
			dataType: 'json',
			header: getHeaders(),
			success: (res) => {
				resolve(res.data);
			},
			fail:(response) => {
				reject(response)
			}
		})
	})
}

function setUserInfo(userInfo){
	uni.setStorageSync("provide_user", userInfo);
}

function getUserInfo(){
	return uni.getStorageSync("provide_user");
}

function getRequest(url, param){
	return request(url, "GET", param);
}

function postRequest(url, param){
	return request(url, "POST", param);
}

export default {
	getToken,
	setToken,
	showTip,
	getRequest,
	postRequest,
	getWebSocketUrl,
	setUserInfo,
	getUserInfo,
	setupIp,
	getIp,
	composeUrl
}